using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Acms.WebControls
{
    public class CommandField : DataControlField
    {
        protected override DataControlField CreateField()
        {
            return new CommandField();
        }

        private ButtonType buttonType;
        public ButtonType ButtonType
        {
            get { return buttonType; }
            set { buttonType = value; }
        }

        private string text;
        public string Text
        {
            get { return text; }
            set { text = value; }
        }

        private string commandName;
        public string CommandName
        {
            get { return commandName; }
            set { commandName = value; }
        }

        private string confirmMessage;
        public string ConfirmMessage
        {
            get { return confirmMessage; }
            set { confirmMessage = value; }
        }

        private string[] confirmMessageDataFields;
        [Editor("System.Web.UI.Design.WebControls.DataFieldEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(System.Drawing.Design.UITypeEditor))]
        [TypeConverter(typeof(StringArrayConverter))]
        public string[] ConfirmMessageDataFields
        {
            get { return confirmMessageDataFields; }
            set { confirmMessageDataFields = value; }
        }

        private bool readOnlyVisible = true;
        public bool ReadOnlyVisible
        {
            get { return readOnlyVisible; }
            set { readOnlyVisible = value; }
        }

        private bool editVisible = true;
        public bool EditVisible
        {
            get { return editVisible; }
            set { editVisible = value; }
        }

        private bool selectedVisible = true;
        public bool SelectedVisible
        {
            get { return selectedVisible; }
            set { selectedVisible = value; }
        }
	
        private string imageUrl;
        public string ImageUrl
        {
            get { return imageUrl; }
            set { imageUrl = value; }
        }

        private bool causesValidation;
        public bool CausesValidation
        {
            get { return causesValidation; }
            set { causesValidation = value; }
        }

        private string validationGroup;
        public string ValidationGroup
        {
            get { return validationGroup; }
            set { validationGroup = value; }
        }
	

        public override void InitializeCell(DataControlFieldCell cell, DataControlCellType cellType, DataControlRowState rowState, int rowIndex)
        {
            if ((rowState & DataControlRowState.Edit) > 0)
            {
                cell.Visible = EditVisible;
            }
            else if ((rowState & DataControlRowState.Insert) > 0)
            {
                cell.Visible = InsertVisible;
            }
            else if ((rowState & DataControlRowState.Selected) > 0)
            {
                cell.Visible = SelectedVisible;
            }
            else
            {
                cell.Visible = ReadOnlyVisible;
            }

            if(cell.Visible)
            {
                if (cellType == DataControlCellType.Header)
                {
                    if (!string.IsNullOrEmpty(HeaderText))
                    {
                        cell.Text = HeaderText;
                    }
                }
                else if (cellType == DataControlCellType.DataCell)
                {
                    IButtonControl btn = null;
                    if (ButtonType == ButtonType.Link)
                    {
                        btn = new LinkButton();
                        /*if (!string.IsNullOrEmpty(ConfirmMessage) && DataFields != null && DataFields.Length > 0)
                        {
                            string[] data = new string[ConfirmMessageDataFields.Length];
                            for(int i = 0; i < ConfirmMessageDataFields.Length; i++)
                            {
                                data[i] = DataBinder.Eval(
                            }
                            ((LinkButton)btn).OnClientClick = string.Format("javascript:return confirm('{0}');", ConfirmMessage);
                        }
                        else*/
                        if (!string.IsNullOrEmpty(ConfirmMessage))
                        {
                            ((LinkButton)btn).OnClientClick = string.Format("javascript:return confirm('{0}');", ConfirmMessage);
                        }
                        /*else if (DataFields != null && DataFields.Length > 0)
                        {
                        }*/
                    }
                    else if (ButtonType == ButtonType.Button)
                    {
                        btn = new Button();
                        if (!string.IsNullOrEmpty(ConfirmMessage))
                        {
                            ((Button)btn).OnClientClick = string.Format("javascript:return confirm('{0}');", ConfirmMessage);
                        }
                    }
                    else if (ButtonType == ButtonType.Image)
                    {
                        btn = new ImageButton();
                        ((ImageButton)btn).ImageUrl = ImageUrl;
                        ((ImageButton)btn).ToolTip = Text;
                        if (!string.IsNullOrEmpty(ConfirmMessage))
                        {
                            ((ImageButton)btn).OnClientClick = string.Format("javascript:return confirm('{0}');", ConfirmMessage);
                        }
                    }
                    cell.DataBinding += new EventHandler(cell_DataBinding);
                    cell.Controls.Add((Control)btn);
                }
            }
        }

        void cell_DataBinding(object sender, EventArgs e)
        {
            DataControlFieldCell cell = sender as DataControlFieldCell;
            IButtonControl btn = cell.Controls[0] as IButtonControl;
            int rowIndex = -1;

            btn.Text = Text;
            btn.CommandName = CommandName;
            btn.CausesValidation = CausesValidation;
            btn.ValidationGroup = ValidationGroup;

            object dataItem = null;
            if (cell.Parent is GridViewRow)
            {
                btn.CommandArgument = ((GridViewRow)cell.Parent).RowIndex.ToString();
                dataItem = ((GridViewRow)cell.Parent).DataItem;
            }
            else if (cell.Parent is DetailsViewRow)
            {
                btn.CommandArgument = ((DetailsViewRow)cell.Parent).RowIndex.ToString();
                dataItem = ((DetailsView)cell.Parent.Parent).DataItem;
            }

            if(!string.IsNullOrEmpty(ConfirmMessage) || (ConfirmMessageDataFields != null && ConfirmMessageDataFields.Length > 0))
            {
                string tmpConfirm = ConfirmMessage;
                if (ConfirmMessageDataFields != null && ConfirmMessageDataFields.Length > 0)
                {
                    int length = ConfirmMessageDataFields.Length;
                    object[] tmpData = new string[length];
                    for (int i = 0; i < tmpData.Length; i++)
                    {
                        tmpData[i] = DataBinder.Eval(dataItem, ConfirmMessageDataFields[i]);
                        if (string.IsNullOrEmpty(ConfirmMessage))
                        {
                            tmpConfirm = string.Concat(tmpConfirm, "{", i.ToString(), "} ");
                        }
                    }
                    tmpConfirm = string.Format(tmpConfirm, tmpData);
                }

                if (btn is Button)
                {
                    ((Button)btn).OnClientClick = string.Format("javascript:return confirm(\"{0}\");", tmpConfirm);
                }
                else if (btn is LinkButton)
                {
                    ((LinkButton)btn).OnClientClick = string.Format("javascript:return confirm(\"{0}\");", tmpConfirm);
                }
                else if (btn is ImageButton)
                {
                    ((ImageButton)btn).OnClientClick = string.Format("javascript:return confirm(\"{0}\");", tmpConfirm);
                }

            }
        }

    }
}
